﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiaosFix.Core
{
    public class RequestAreaOperator
    {

        private DiaosFixEntities context = new DiaosFixEntities();

        public Response<List<RequestArea>> FindAllRequestAreas()
        {
            var list = context.RequestArea.ToList();
            if (null != list)
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = true,
                    Message = string.Empty,
                    Item = list
                };
            }
            else
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = false,
                    Message = "query return a null value for request areas",
                    Item = null
                };
            }
        }

        public Response<RequestArea> AddNewArea(string areaName)
        {
            var response = new Response<RequestArea>();
            var area = context.RequestArea.Where(ca => ca.AreaName == areaName);
            if (area.SingleOrDefault() == null)
            {
                context.AddRequestAreaByAreaNameAndGetNewResults(areaName);
                var q = context.RequestArea.Where(ca => ca.AreaName == areaName).SingleOrDefault();
                response.IsSuccess = true;
                response.Message = "Successful!";
                response.Item = q;
                return response;
            }
            else
            {
                response.Item = null;
                response.Message = "Exists!";
                response.IsSuccess = false;
            }
            context.SaveChanges();
            return response;
        }

        public Response<object> DeleteArea(int areaId)
        {
            Response<object> response = new Response<object>();
            var q = context.DeleteRequestAreaByRequestAreaId(areaId);
            if (q > 0)
            {
                response.IsSuccess = true;
                response.Message = "Delete Successful!";
            }
            else
            {
                response.IsSuccess = false;
                response.Message = "Delete Failed!";
            }
            context.SaveChanges();
            return response;
        }

        public Response<object> DeleteArea(string areaName)
        {
            Response<object> response = new Response<object>();
            int q = (from a in context.RequestArea where a.AreaName == areaName select a.Id).SingleOrDefault();
            if (null != q)
            {
                context.SaveChanges();
                return this.DeleteArea(q);
            }
            else
            {
                response.Message = "Delete Failed!";
                response.IsSuccess = false;
                context.SaveChanges();
                return response;
            }
        }

        public Response<RequestArea> UpdateAreaInfo(RequestArea area)
        {
            throw new System.NotImplementedException();
        }

        public Response<List<Requests>> FindAllRequestsIn(string areaName)
        {
            var response = new Response<List<Requests>>();
            int? areaid = (from fuck in context.RequestArea where fuck.AreaName == areaName select fuck.Id).SingleOrDefault();
            if (areaid != null)
            {
                var q = context.Requests.Where(p => p.RequestAreaId == areaid).ToList();
                if (null != q)
                {
                    response.Message = "Successful";
                    response.IsSuccess = true;
                    response.Item = q;
                }
                else
                {
                    response.Message = "NoResults";
                    response.IsSuccess = false;
                    response.Item = null;
                }
            }
            else
            {
                response.Message = "AreName not exists!";
                response.IsSuccess = false;
                response.Item = null;
            }
            return response;
        }

        public Response<int> DeleteAllRequestsIn(string areaName)
        {
            throw new System.NotImplementedException();
        }
    }
}
